package com.huiqiao.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.bus.domain.Rent;
import com.huiqiao.bus.mapper.RentMapper;
import com.huiqiao.bus.service.IRentService;
import com.huiqiao.bus.vo.RentVo;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;
import java.util.Objects;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-19  16:34
 */

@Service
@Transactional //事务控制
public class RentServiceImpl implements IRentService{

    @Autowired
    private RentMapper rentMapper;

    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        Weekend<Rent> rentWeekend = Weekend.of(Rent.class);
        WeekendCriteria<Rent, Object> rentObjectWeekendCriteria = rentWeekend.weekendCriteria();
        //出租单号 rentid
        if (StrUtil.isNotBlank(rentVo.getRentid())){
            rentObjectWeekendCriteria.andLike(Rent::getRentid, "%" + rentVo.getRentid() + "%");
        }
        //身份证号 identity
        if (StrUtil.isNotBlank(rentVo.getIdentity())){
            rentObjectWeekendCriteria.andLike(Rent::getIdentity, "%" + rentVo.getIdentity() + "%");
        }
        //车牌号 carnumber
        if(StrUtil.isNotBlank(rentVo.getCarnumber())){
            rentObjectWeekendCriteria.andLike(Rent::getCarnumber, "%" + rentVo.getCarnumber() + "%");
        }
        //出租开始时间 startTime & 出租结束时间 endTime
        if (Objects.nonNull(rentVo.getStartTime()) && Objects.nonNull(rentVo.getEndTime())){
            rentObjectWeekendCriteria.andBetween(Rent::getBegindate, rentVo.getStartTime(), rentVo.getEndTime());
        }
        //出租单状态 rentflag
        if (Objects.nonNull(rentVo.getRentflag())){
            rentObjectWeekendCriteria.andEqualTo(Rent::getRentflag, rentVo.getRentflag());
        }
        List<Rent> data = rentMapper.selectByExample(rentWeekend);
        return new DataGridView(page.getTotal(), data);

    }

    @Override
    public Rent querCustomerByRentId(String rentid) {
        return rentMapper.selectByPrimaryKey(rentid);
    }

    @Override
    public int updateRent(RentVo rentVo) {
        try {
            this.rentMapper.updateByPrimaryKeySelective(rentVo);
            return 0;
        }catch (Exception e ){
            System.out.println("e = " + e);
            return -1;
        }
    }

    @Override
    public Rent queryRentByRentId(String rentid) {
        return rentMapper.selectByPrimaryKey(rentid);
    }

    @Override
    public List<Rent> queryAllRents() {
        return rentMapper.selectAll();
    }

}
